package top.mcwebsite.lib.serviceManager.plugin

import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.ClassWriter
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Opcodes

class InjectServiceProviderCodeClassVisitor(
    classWriter: ClassWriter,
    private val serviceProviders: Set<String>
) : ClassVisitor(Opcodes.ASM9, classWriter) {

    override fun visitMethod(
        access: Int,
        name: String?,
        descriptor: String?,
        signature: String?,
        exceptions: Array<out String>?
    ): MethodVisitor {
        val oldVisitor = super.visitMethod(access, name, descriptor, signature, exceptions)
        return if (name == "inject") {
            ServiceProviderInjectMethodVisitor(oldVisitor, serviceProviders, access, name, descriptor)
        } else {
            oldVisitor
        }
    }
}